package link.anyauto.weclass.demos.viewpager;

import android.app.Activity;
import android.databinding.ObservableFloat;
import android.databinding.ObservableInt;
import android.support.v4.view.ViewPager;

import java.util.ArrayList;
import java.util.List;

import link.anyauto.weclass.R;
import link.anyauto.weclass.adapter.OneTypeItemSource;
import link.anyauto.weclass.adapter.SomethingClicked;
import link.anyauto.weclass.base.BaseVMAdapter;
import link.anyauto.weclass.tab.TabItem;

/**
 * Created by Administrator on 2017/2/19.
 */

public class PagerTabVM extends BaseVMAdapter {
    public List<TabItem> tabs = new ArrayList<>();
    public ObservableInt selected = new ObservableInt(0);
    public ObservableFloat tabBgPos = new ObservableFloat(0);
    public ViewPager.OnPageChangeListener listener;
    public OneTypeItemSource pageSource;
    public SomethingClicked selectListener = new SomethingClicked() {
        @Override
        public void clicked(int position) {
            selected.set(position);
        }
    };

    public PagerTabVM(Activity activity) {
        setActivity(activity);
        initTabs();
        initPageSource();
        initListener();
        title(R.string.view_pager_tab_demo);
    }

    void initTabs() {
        for(int i=0; i<3; i++) {
            TabItem ti = new TabItem();
            ti.pos = i;
            ti.title = "Tab " + (i + 1);
            ti.listener = selectListener;
            tabs.add(ti);
        }
    }

    void initPageSource() {
        pageSource = new OneTypeItemSource();
        pageSource.itemViewId = R.layout.demo_view_pager_item;
        for (int i=0; i<3; i++) {
            PagerItem item = new PagerItem();
            item.title = "这是第" + i + "页标题";
            item.content = "这里是第" + i + "页Pager的内容。欢迎浏览，祝你生活愉快！";
            pageSource.items.add(item);
        }
    }

    void initListener() {
        listener = new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                tabBgPos.set(position + positionOffset);
            }

            @Override
            public void onPageSelected(int position) {
                selected.set(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        };
    }
}
